/**
 * @author ZCR
 * @create 2025/8/16 21:38
 */
public class Solution43 {
    public String multiply(String num1, String num2) {
        StringBuilder result = new StringBuilder("0");
        int count = 0;
        for (int i = num2.length() - 1; i >= 0; i--) {
            result = new StringBuilder(add(result.toString(),multiply(num1,num2.charAt(i),count++)));
        }
        while (result.charAt(0) == '0' && result.length() > 1) {
            result.deleteCharAt(0);
        }
        return result.toString();
    }

    public String multiply(String num1, Character num2, Integer k) {
        StringBuilder result = new StringBuilder();
        int carry = 0;
        for (int i = num1.length() - 1; i >= 0; i--) {
            int sum = (num1.charAt(i) - '0') * (num2 - '0') + carry;
            result.append(sum % 10);
            carry = sum / 10;
        }
        if (carry > 0) {
            result.append(carry);
        }
        result = result.reverse();
        while (k-- > 0) {
            result.append("0");
        }
        return result.toString();
    }

    public String add(String num1, String num2) {
        StringBuilder result = new StringBuilder();
        int carry = 0;
        int index1 = num1.length() - 1, index2 = num2.length() - 1;
        while (index1 >= 0 || index2 >= 0) {
            int a = index1 >= 0 ? num1.charAt(index1) - '0' : 0;
            int b = index2 >= 0 ? num2.charAt(index2) - '0' : 0;
            int sum = (a + b + carry) % 10;
            carry = (a + b + carry) / 10;
            index1--;
            index2--;
            result.append(sum);
        }
        if (carry != 0) {
            result.append(carry);
        }
        return result.reverse().toString();
    }

    public static void main(String[] args) {
        Solution43 solution = new Solution43();
        System.out.println(solution.multiply("123", "456"));
        System.out.println(solution.multiply("2", "3"));
        System.out.println(solution.multiply("9133", "0"));
    }
}
